package com.wyzz.hr.support;

import java.util.*;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ComparatorChain;
import org.apache.commons.lang.StringUtils;

public class RecordSorter {

	public RecordSorter(List records) {
		this.records = records;
	}

	public void doSort(String sortProperty, String order) {
		if (records == null)
			records = new ArrayList(0);
		if (StringUtils.isEmpty(sortProperty))
			sortProperty = "examinDate";
		if (StringUtils.isEmpty(order))
			order = "up";
		java.util.Comparator mycmp = ComparatorUtils
				.nullLowComparator(ComparableComparator.getInstance());
		if ("down".equalsIgnoreCase(order))
			mycmp = ComparatorUtils.reversedComparator(mycmp);
		ArrayList sortFields = new ArrayList();
		sortFields.add(new BeanComparator(sortProperty, mycmp));
		java.util.Comparator empNameSort = ComparatorUtils
				.nullLowComparator(ComparableComparator.getInstance());
		sortFields.add(new BeanComparator("empName", empNameSort));
		java.util.Comparator examinDateSort = ComparatorUtils
				.nullLowComparator(ComparableComparator.getInstance());
		sortFields.add(new BeanComparator("examinDate", examinDateSort));
		ComparatorChain multiSort = new ComparatorChain(sortFields);
		Collections.sort(records, multiSort);
	}

	private List records;
	private static final String DEFAULT_SORT_PROPERTY = "examinDate";
	private static final String DEFAULT_SORT_ORDER = "up";
}
